import '../../util/_base_export.dart';
import '../../util/_view_export.dart';

import '../../blocs/code.dart';
import '../../blocs/cart.dart';
import '../../util/app_util.dart';
import '../../blocs/product.dart';

class AccountSwitchPage extends StatefulWidget {
  // final String scenario;
  const AccountSwitchPage(
      // this.scenario,
      {
    Key? key,
  }) : super(key: key);
  @override
  _AccountSwitchPageState createState() => _AccountSwitchPageState();
}

class _AccountSwitchPageState extends State<AccountSwitchPage> {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance?.addPostFrameCallback((callback) {});
  }

  List<Widget> list = List<Widget>.empty(growable :true);

  @override
  Widget build(BuildContext context) {
    CartBloc _cart = Provider.of<CartBloc>(context, listen: false);

    return Scaffold(
      appBar: AppBar(
        brightness: Brightness.light,
        leading: GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: () {
            Navigator.pop(context);
          },
          child: Container(
            child: Icon(
              Icons.chevron_left,
              color: AppColor.blackMain,
              size: 32,
            ),
          ),
        ),
        title: Text(
          AppText.text['account_switch'],
          style: TextStyle(color: AppColor.blackMain),
        ),
      ),
      body: SingleChildScrollView(
        child: Container(
          color: Colors.grey[50],
          margin: EdgeInsets.only(top: 20.h),
          child: _bodyBlock(context, _cart),
        ),
      ),
    );
  }

  Widget _bodyBlock(BuildContext context, CartBloc ib_cart) {
    print(AppSession.wholesale_wh_id);
    print(AppSession.appUtil.wholesale_wh_id);
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 30.w),
      child: Column(children: <Widget>[
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text(
              '请选择一个账户',
              style: AppTextStyle.code1Referal,
            ),
            Text(
              '注意： 不同账户对应的产品内容和价格会有差异，切换账户后购物车内容会自动清空，请仔细核对',
              style: AppTextStyle.notesSwitch,
            ),
          ],
        ),
        Column(
          children: <Widget>[
            Padding(
              padding: EdgeInsets.only(top: 60.h, bottom: 10.h),
              child: AppButton.accountSwitch('个人零售账户', Color(0xffFF5858), () async {
                AppSession.wholesale_wh_id = 0;

                AppPopup.popupToast('I', '切换个人零售账户成功');
                App.router.navigateTo(context, AppRoutes.appMain, replace: true);
              }),
            ),
            Text(
              '个人帐户满足个人家庭生活采购',
              style: AppTextStyle.notesButtonSwitch,
            ),
          ],
        ),
        Column(
          children: <Widget>[
            Padding(
              padding: EdgeInsets.only(top: 60.h, bottom: 10.h),
              child: AppButton.accountSwitch('团购批发账户', Color(0xff7655FF), () async {
                print('pressed');
                if (AppSession.appUtil.wholesale_wh_id == 0)
                  AppPopup.popupToast('W', '未开通团购批发账户');
                else {
                  // AppSession.wholesale_wh_id = AppSession.appUtil.wholesale_wh_id;
                  // Provider.of<ProductBloc>(context, listen: false).loadData();
                  // AppPopup.popupToast('S', '切换团购批发账户成功，请返回');
                  // Navigator.of(context).pop();
                  AppSession.wholesale_wh_id = AppSession.appUtil.wholesale_wh_id;

                  AppPopup.popupToast('I', '切换团购批发账户成功');
                  Navigator.pushReplacementNamed(
                    context,
                    AppRoutes.appMain,
                  );
                  // App.router.navigateTo(context, AppRoutes.appMain, replace: true);
                }
              }),
            ),
            Text(
              '团购批发账户为批量采购提供便利',
              style: AppTextStyle.notesButtonSwitch,
            ),
            if (AppSession.appUtil.wholesale_wh_id == 0)
              GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: () {
                  App.router.navigateTo(context, AppRoutes.accountWholesaleApply);
                },
                child: Text(
                  '还未注册，点击申请',
                  style: AppTextStyle.urlButtonSwitch,
                ),
              ),
          ],
        ),
      ]),
    );
  }
}
